package com.example.zhapp_java.activities;

import android.content.Intent;
import android.os.Bundle;
import android.widget.SearchView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zhapp_java.R;
import com.example.zhapp_java.adapters.ProjectAdapter;
import com.example.zhapp_java.api.ApiClient;
import com.example.zhapp_java.api.ApiService;
import com.example.zhapp_java.models.Project;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

// MainActivity.java
public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private ProjectAdapter adapter;
    private List<Project> projectList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化RecyclerView
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ProjectAdapter(projectList, this::onProjectClick);
        recyclerView.setAdapter(adapter);

        // 设置搜索功能
        SearchView searchView = findViewById(R.id.searchView);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                loadProjects(query);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if (newText.isEmpty()) loadProjects("");
                return false;
            }
        });

        // 添加项目按钮
        findViewById(R.id.fabAdd).setOnClickListener(v -> showAddProjectDialog());

        // 加载初始数据
        loadProjects("");
    }

    private void loadProjects(String search) {
        ApiService api = ApiClient.getClient().create(ApiService.class);
        Call<List<Project>> call = api.getProjects(search);
        call.enqueue(new Callback<List<Project>>() {
            @Override
            public void onResponse(Call<List<Project>> call, Response<List<Project>> response) {
                if (response.isSuccessful()) {
                    projectList.clear();
                    projectList.addAll(response.body());
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<List<Project>> call, Throwable t) {
                Toast.makeText(MainActivity.this, "加载失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void onProjectClick(Project project) {
        Intent intent = new Intent(this, ProjectDetailActivity.class);
        intent.putExtra("PROJECT_NO", project.getProjectNo());
        startActivity(intent);
    }

    private void showAddProjectDialog() {
        // 实现添加项目的对话框
    }
}